package com.car.biz;

import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.car.dao.AutoHomeDao;
import com.car.entity.AutoIowmEntity;
import com.car.util.Util;

public class AutoHomeBiz {
	static Logger logger = Logger.getLogger(AutoHomeBiz.class.getName());
	private AutoHomeDao autoHomeDao = new AutoHomeDao();
//	List<String> iwomList = new ArrayList<String>();
//	List<String> bbsList = new ArrayList<String>();
//	List<List<String>>[] list = new List[2];

	public boolean addKb(Document doc) {
		logger.info("into addkb");
		boolean insert = false;
		logger.info(doc);
		Elements eles = doc.select("div.mouthcon");
		for (Element e : eles) {
			logger.info("into for");
			try {

				Element tag01 = e.select("div.mouth-main div.cont-title span.time a").get(0);
				String kburl = tag01.attr("href").split("_")[1].trim();
				int kb_id = Integer.parseInt(kburl);// 口碑id
				Element tag03 = e.select("div.mouthcon-cont-left div.name-text a").get(0);
				String kb_yhmc = tag03.text();// 用户名称
				String yhid = tag03.attr("href").split("cn/")[1].trim();
				int kb_yhid = Integer.parseInt(yhid);// 用户id
				Element tag04 = e.select("div.mouth-main div.cont-title span.time a").get(0);
				String plrq = tag04.text().split("发表")[0].trim();
				Date kb_plrq = (Date) Util.getDate(plrq);// 评论日期
				String kb_gmqy = "";// 购买区域
				String kb_jxs = "";// 经销商
				float item[] = new float[8];
				Elements tag06 = e.select("div.position-r span.c333");
				for (int i = 0; i < tag06.size(); i++) {
					Element t06 = tag06.get(i);
					item[i] = Float.parseFloat(t06.text());
				}
				Elements chooses = e.select("div.mouthcon-cont-left").select("div.choose-con")
						.select("div.mt-10>dl.choose-dl:not(.border-b-no)");
				Map<String, String> box = new HashMap<String, String>();
				for (Element c : chooses) {
					String key = c.select("dt").text();
					String value = "";
					switch (key) {
					case "购买车型":
						String ppid01 = c.select("dd a").get(0).attr("href").split("/")[1];
						String cxid01 = c.select("dd a").get(1).attr("href").split("/")[2];
						value = ppid01 + "-" + cxid01;
						break;
					case "购买地点":
						String dd = c.select("dd.c333").text().trim();
						value = dd;
						break;
					case "购买时间":
						String sj = c.select("dd.bg-blue").get(0).text();
						String nian = sj.split("年")[0];
						String yue = sj.substring(5, sj.indexOf("月"));
						String gmsjTag = nian + "-" + yue + "-" + "01";
						value = gmsjTag;
						break;
					case "裸车购买价":
						String tag = c.select("dd.font-arial").select("dd.bg-blue").text();
						String lcj = tag.split("万")[0];
						value = lcj.substring(0, lcj.length() - 1);
						break;
					case "油耗 目前行驶":
						Element e1 = c.select("dd.bg-blue p").get(0);
						Element e2 = c.select("dd.bg-blue p").get(1);
						String syyh = e1.text().split("升/百公里")[0].substring(0, e1.text().indexOf("升") - 1);
						String lc = e2.text().split("公里")[0].substring(0, e2.text().indexOf("公") - 1);
						value = syyh + "-" + lc;
						break;
					default:
						break;
					}
					box.put(key, value);
				}
				int kb_ppid = Integer.parseInt(box.get("购买车型").split("-")[0]);// 品牌id
				int kb_cxid = Integer.parseInt(box.get("购买车型").split("-")[1]);// 系列id
				String kb_gmdd = box.get("购买地点");// 购买地点
				Date kb_gmsj = (Date) Util.getDate(box.get("购买时间"));// 购买时间
				float kb_gmjg = Float.parseFloat(box.get("裸车购买价"));// 购买价格
				float kb_syyh = 0;
				int kb_lc = 0;
				try {
					kb_syyh = Float.parseFloat(box.get("油耗 目前行驶").split("-")[0]);
					kb_lc = Integer.parseInt(box.get("油耗 目前行驶").split("-")[1]);
				} catch (Exception e1) {
					java.util.Date date = new java.util.Date();
					DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
					System.err.println("kb_cxid=" + kb_cxid + "无油耗和行驶记录！" + "\t" + format.format(date));
				}
				float kb_kj = item[0];// 空间
				float kb_dl = item[1];// 动力
				float kb_ck = item[2];// 操控
				float kb_yh = item[3];// 油耗
				float kb_ssx = item[4];// 舒适性
				float kb_wg = item[5];// 外观
				float kb_ns = item[6];// 内室
				float kb_xjb = item[7];// 性价比
				Element tag07 = e.select("div.text-cont").get(0);
				String kb_plnr = tag07.text();// 评论内容
				int kb_plcd = tag07.text().length();// 评论长度
				String zcs = e.select("label.supportNumber").get(0).text();
				int kb_zcs = Integer.parseInt(zcs);// 支持数
				String lls = e.select("a.orange").text();
				int kb_lls = Integer.parseInt(lls);// 浏览数
				Element tag12 = e.select("span.font-arial").select("span.CommentNumber").get(0);
				String pls = tag12.text();
				int kb_pls = Integer.parseInt(pls);// 评论数
				int kb_ztsl = 0;//
				java.util.Date zqsj = new java.util.Date();
				java.sql.Date kb_zqsj = new Date(zqsj.getTime());// 抓取时间
				AutoIowmEntity autoIowmEntity = new AutoIowmEntity(kb_id, kb_ppid, kb_cxid, kb_yhmc, kb_yhid, kb_plrq, kb_gmdd, kb_gmqy, kb_jxs, kb_kj,
						kb_dl, kb_ck, kb_yh, kb_ssx, kb_wg, kb_ns, kb_xjb, kb_plnr, kb_plcd, kb_gmsj, kb_gmjg, kb_syyh,
						kb_lc, kb_zcs, kb_lls, kb_pls, kb_ztsl, kb_zqsj);
				logger.info("123");
				insert = autoHomeDao.addCarIowm(autoIowmEntity);
				if (insert) {
					logger.info("insert kb success!");
				}else{
					logger.info("insert kb wrong!");
				}
				

			} catch (Exception a) {
				a.printStackTrace();

			}
			

		}
		return insert;

	}

	



}
